package com.iflytek.controller;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.iflytek.domain.User;
import com.iflytek.service.UserService;
import com.iflytek.utils.VerifyCodeUtils;

@Controller
//@RequestMapping(value="/login")
public class LoginController {
	
	@Autowired
	private UserService us;
	
	// 将登陆验证码发送到session域当中
	@RequestMapping(value = "/login/sendVerifyCode.action")
	public void sendVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");
		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute("rand", verifyCode.toLowerCase());
		// 生成图片
		int w = 70, h = 35;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
	}
	
	// ajax异步校验数据库中是否存在该手机号码
	@RequestMapping(value = "/login/isMobile.action")
	public void isMobile(String userphone, HttpServletResponse response) throws IOException{
		boolean isExist = us.findPhoneNumberIsRegisted(userphone);
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
	}
	
	// 校验登陆验证码是否正确
	@RequestMapping(value = "/login/verificationCode.action")
	public void verificationCode(String logincode,HttpSession session,HttpServletResponse response)throws IOException {
		String code1 = (String) session.getAttribute("rand");
		boolean isExist = true;
		String code2 = logincode.toLowerCase();
		if (logincode != null && code1 != null) {
			if (code1.equals(code2)) {
				isExist = true;
			} else {
				//isExist = false;为方便调试验证码不管全为true
				isExist = true;
			}
		} else {
			//isExist = false;
			isExist = true;
		}
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
	}
	
	// 校验手机号与密码是否匹配
	@RequestMapping(value = "/login/verification.action")
	public String verification(String userphone,String password,HttpServletRequest request, HttpServletResponse response)throws IOException {
		HttpSession session = request.getSession();
		boolean isExist = us.findUserIsVerification(userphone, password);
		String json = "{\"isExist\":" + isExist + "}";
		response.getWriter().write(json);
		if (isExist) {
			User user = us.getUserByPhoneNumber(userphone);
			String autoLogin = request.getParameter("autoLogin");
			if ("autoLogin".equals(autoLogin)) {
				Cookie cookie_userphone = new Cookie("cookie_username", user.getUserphone());
				cookie_userphone.setMaxAge(10 * 60);
				Cookie cookie_password = new Cookie("cookie_password", user.getPassword());
				cookie_password.setMaxAge(10 * 60);
				response.addCookie(cookie_userphone);
				response.addCookie(cookie_password);
			}
			session.setAttribute("user", user);
			return "redirect:index";
		} else {
			request.setAttribute("loginError", "用户名或密码错误");
			return "forward:login";
		}
	}
	
	// 注销用户登录
	@RequestMapping(value = "/login/cancellation.action")
	public String cancellation(HttpSession session, HttpServletResponse response) throws IOException {
		session.removeAttribute("user");
		return "redirect:index";
	}
}
